package com.jnu.keepaccounts;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jnu.keepaccounts.data.DataTiaoMu;
import com.jnu.keepaccounts.data.XuanKa;

import java.util.ArrayList;
import java.util.Calendar;

public class InputActivity extends AppCompatActivity {
    private ArrayList<XuanKa> xuanKas;
    private XuankaAdapter xuankaAdapter;
    int flag = -1;
    int pictureId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);

        TextView textView = findViewById(R.id.textview_xiangmu_xuanze);
        EditText editText = findViewById(R.id.edittext_shue);
        Button button_riqi = findViewById(R.id.button_riqi);


        final int[] year = new int[1];
    final int[] month = new int[1];
    final int[] day = new int[1];
    Intent intent = getIntent();
    int position = 0;
    DataTiaoMu dataTiaoMu = (DataTiaoMu) intent.getSerializableExtra("tiaomu");
        if(dataTiaoMu!=null ){
        position = intent.getIntExtra("position",0);
        textView.setText(dataTiaoMu.getXiangMu());
        editText.setText(Math.abs(dataTiaoMu.getShuE())+"");
        year[0]=dataTiaoMu.getYear();
        month[0] = dataTiaoMu.getMonth();
        day[0] = dataTiaoMu.getDay();
        button_riqi.setText(year[0]+"-"+month[0]+"-"+day[0]);
        pictureId = dataTiaoMu.getImage();
    }

        this.xuanKas= intent.getExtras().getParcelableArrayList("xuanKas");

    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView_xuanka);
    GridLayoutManager layoutManager = new GridLayoutManager(this,5);
        recyclerView.setLayoutManager(layoutManager);
    xuankaAdapter = new XuankaAdapter(xuanKas);
        recyclerView.setAdapter(xuankaAdapter);
        xuankaAdapter.setOnItemClickListener(new XuankaAdapter.OnItemClickListener(){
        @Override
        public void onItemClicked(View view, int position) {
            textView.setText(xuanKas.get(position).getName());
            flag = xuanKas.get(position).getFlag();
            pictureId = xuanKas.get(position).getPictureId();
        }
    });

    Button button_baocun = findViewById(R.id.button_baocun);


    //日期选择
        if(button_riqi.getText()==null){
        Calendar calendar = Calendar.getInstance();
        year[0] = calendar.get(Calendar.YEAR);
        month[0] = calendar.get(Calendar.MONTH)+1;
        day[0] = calendar.get(Calendar.DAY_OF_MONTH);
        button_riqi.setText(year[0] +"-"+ month[0] +"-"+ day[0]);
    }
        button_riqi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //通过自定义控件AlertDialog实现
            AlertDialog.Builder builder = new AlertDialog.Builder(InputActivity.this);
            @SuppressLint("ResourceType") View view = (LinearLayout) getLayoutInflater().inflate(R.layout.date, null);
            final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
            //设置日期简略显示 否则详细显示 包括:星期\周
            datePicker.setCalendarViewShown(false);
            //设置date布局
            builder.setView(view);
            builder.setPositiveButton("确 定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //日期格式
                    year[0] = datePicker.getYear();
                    month[0] = datePicker.getMonth()+1;
                    day[0] = datePicker.getDayOfMonth();
                    button_riqi.setText(year[0] +"-"+ month[0] +"-"+ day[0] );
                    dialog.cancel();
                }
            });
            builder.setNegativeButton("取 消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.create().show();
        }
    });

    int finalPosition = position;
        button_baocun.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("year",year[0]);
            intent.putExtra("month",month[0]);
            intent.putExtra("day",day[0]);
            intent.putExtra("shuE",flag*Double.parseDouble(String.valueOf(editText.getText())));
            intent.putExtra("Name",(String) textView.getText());
            intent.putExtra("pictureId",pictureId);
            intent.putExtra("source","微信");
            intent.putExtra("position", finalPosition);
            setResult(1,intent);
            InputActivity.this.finish();
        }
    });
}
public static class XuankaAdapter extends RecyclerView.Adapter<InputActivity.XuankaAdapter.ViewHolder>{

    private ArrayList<XuanKa> xuanKas;
    private InputActivity.XuankaAdapter.OnItemClickListener onItemClickListener;
    public interface OnItemClickListener{
        void onItemClicked(View view,int position);
    }
    public void setOnItemClickListener(InputActivity.XuankaAdapter.OnItemClickListener clickListener){
        this.onItemClickListener = clickListener;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView xuanka;
        TextView text;

        public ViewHolder (View view,final InputActivity.XuankaAdapter.OnItemClickListener onItemClickListener)
        {
            super(view);
            xuanka = (ImageView) view.findViewById(R.id.xuanka_name);
            text = (TextView)  view.findViewById(R.id.text_name);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener!= null){
                        int position = getAdapterPosition();
                        if(position!= RecyclerView.NO_POSITION){
                            onItemClickListener.onItemClicked(view ,position);
                        }
                    }
                }
            });
        }

    }
    public XuankaAdapter(ArrayList<XuanKa> xuanKas){
        this.xuanKas = xuanKas;
    }
    @NonNull
    @Override
    public InputActivity.XuankaAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.xuanka_holder,parent,false);
        InputActivity.XuankaAdapter.ViewHolder holder = new InputActivity.XuankaAdapter.ViewHolder(view,onItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull InputActivity.XuankaAdapter.ViewHolder holder, int position) {
        XuanKa xuanKa = xuanKas.get(position);
        holder.xuanka.setBackgroundResource(xuanKa.getPictureId());
        holder.text.setText(xuanKa.getName());
    }

    @Override
    public int getItemCount() {
        return xuanKas.size();
    }
}
}